	* Drop observations for Eritrea before 1993 and South Sudan before 2011
	
		drop if gwnoloc==531 & year<1993
		drop if gwnoloc==626 & year<2011

		

	* Generate lagged dependent variables
		
		sort gwnoloc year
		
		foreach x of varlist v2x_polyarchy polity2 fh_pr_rec democracy ///
			{
		cap	gen `x'_1l=`x'[_n-1]
		cap	gen `x'_2l=`x'[_n-2]
		cap	gen `x'_3l=`x'[_n-3]
		cap	gen `x'_4l=`x'[_n-4]
		cap	gen `x'_5l=`x'[_n-5]
			
			local lab: variable label `x'
			
			la var `x'_1l "`lab' (1 yr lag)"
			la var `x'_2l "`lab' (2 yr lag)"
			la var `x'_3l "`lab' (3 yr lag)"
			la var `x'_4l "`lab' (4 yr lag)"
			la var `x'_5l "`lab' (5 yr lag)"
			}

			
			
	* Divide personnel numbers by 1,000

	cap	gen itotal_compound_K = itotal_compound / 1000
			la var itotal_compound_K "\# of uniformed personnel (1K units)"
		
	cap	gen iactual_civilian_total_K = iactual_civilian_total / 1000
			la var iactual_civilian_total_K "\# of civilian personnel (1K units)"
	
	
	
	* Generate lagged independent variables
		
		sort gwnoloc year
		
		foreach x of varlist ///
			itotal_compound_K iactual_civilian_total_K idemo_all_max_dum ivoters_all_max_dum iparties_all_max_dum iany_demo_all_max_dum iany_demo_rel_all_max_dum ielections_all_max_dum iall_demo_max ///
			iany_demo_engage_max_dum iany_demo_bypass_max_dum ipema_any_demo_assist_dum ipema_any_demo_rel_assist_dum ipema_any_demo_agg  iall_demo_engage_max iall_demo_bypass_max ///
				{
				cap	gen `x'_1l=`x'[_n-1]
				cap	gen `x'_2l=`x'[_n-2]
				cap	gen `x'_3l=`x'[_n-3]
				cap	gen `x'_4l=`x'[_n-4]
					
				local lab: variable label `x'
				
				la var `x'_1l "`lab' (1 yr lag)"
				la var `x'_2l "`lab' (2 yr lag)"
				la var `x'_3l "`lab' (3 yr lag)"
				la var `x'_4l "`lab' (3 yr lag)"
				
				}

				
		
	* Set missing values on control variables to within-country means 
				
		foreach x of varlist wdi_pop wdi_oda wdi_gdppc unhcr_ref_idp wdi_literacy wdi_fuel  {
			bys gwnoloc: egen `x'_mean=mean(`x')
		cap	gen i`x'=`x'
		cap	replace i`x'=`x'_mean if `x'==.
				local lab: variable label `x'
				lab var i`x' "`lab' (missing = within-country mean)"
					drop `x'_mean
			}

		
					
	* Generate lagged control variables
		
		sort gwnoloc year
							
		foreach x of varlist ///
			iwdi_pop iwdi_oda iwdi_gdppc iunhcr_ref_idp iwdi_literacy iwdi_fuel ///
			{
		cap	gen `x'_2l=`x'[_n-2]
		cap	gen `x'_3l=`x'[_n-3]
		cap	gen `x'_4l=`x'[_n-4]
		cap	gen `x'_5l=`x'[_n-5]
		cap	gen `x'_6l=`x'[_n-6]

				local lab: variable label `x'
				
				la var `x'_2l "`lab' (2 yr lag)"
				la var `x'_3l "`lab' (3 yr lag)"
				la var `x'_4l "`lab' (4 yr lag)"
				la var `x'_5l "`lab' (5 yr lag)"
				la var `x'_6l "`lab' (5 yr lag)"

			}



	* Generate instruments
								
		sort gwnoloc year
								
		tab gwnoloc, gen(country_temp)
		
	cap	gen ianydemoallmaxdum = iany_demo_all_max_dum 
	cap	gen ipemaanydemoassistdum = ipema_any_demo_assist_dum
				
		foreach x of varlist ianydemoallmaxdum ipemaanydemoassistdum ///
			{ 
	cap		gen `x'_iv = .
			}
		
		foreach x of varlist ianydemoallmaxdum ipemaanydemoassistdum ///
			{ 
			foreach n of numlist 1/42 {
				bys year: egen `x'_`n'=mean(`x') if country_temp`n'!=1
				bys year: replace `x'_`n'=-1 if country_temp`n'==1
				bys year: egen `x'_`n'_max=max(`x'_`n') if `x'_`n'!=.
				}

			foreach n of numlist 1/42 {
				bys year: replace `x'_iv=`x'_`n'_max if country_temp`n'==1
				}						
			}	
					
		foreach x of varlist ianydemoallmaxdum ipemaanydemoassistdum ///
			{ 
				drop `x'_1 - `x'_42_max		
			}			
					
		foreach x of varlist ianydemoallmaxdum ipemaanydemoassistdum ///
			{ 
			foreach n of numlist 1/42 {
				bys year: egen `x'_`n'=mean(`x') if country_temp`n'!=1 & pko_p4p==1
				bys year: replace `x'_`n'=-1 if country_temp`n'==1
				bys year: egen `x'_`n'_max=max(`x'_`n') if `x'_`n'!=.
				}
			}
			
		foreach x of varlist ianydemoallmaxdum ipemaanydemoassistdum ///
			{ 
				drop `x'_1 - `x'_42_max		
				replace `x'_iv = . if `x'_iv == -1
			}	
			
			drop ianydemoallmaxdum ipemaanydemoassistdum
			
		cap	ren ianydemoallmaxdum_iv iany_demo_all_maxdiv
		cap	ren ipemaanydemoassistdum_iv ipema_any_demo_assdiv

			drop country_temp*
				
			la var iany_demo_all_maxdiv "\% democracy activities in other missions" 
			la var ipema_any_demo_assdiv "\% democracy mandate components in other missions" 
							
							
				
	* Generate lagged instruments

		sort gwnoloc year

		foreach x of varlist ///
			iany_demo_all_maxdiv ipema_any_demo_assdiv ///		
				{
			cap		gen `x'_1l=`x'[_n-1]
			cap		gen `x'_2l=`x'[_n-2]
			cap		gen `x'_3l=`x'[_n-2]
					
					local lab: variable label `x'
				
					la var `x'_1l "`lab' (1 yr lag)"
					la var `x'_2l "`lab' (2 yr lag)"
					la var `x'_3l "`lab' (3 yr lag)"
				}

			
									
	* Generate dummies for conflict and post-conflict samples

	cap	gen ucdp_0yrs=ucdp
	
	cap	gen ucdp_1yrs=0
			bys gwnoloc: replace ucdp_1yrs=. if ucdp_ever==0
			bys gwnoloc: replace ucdp_1yrs=1 if year>=ucdp_end_alt+1
			bys gwnoloc: replace ucdp_1yrs=0 if ucdp==1
	
		cap gen ucdp_2yrs=0
			bys gwnoloc: replace ucdp_2yrs=. if ucdp_ever==0
			bys gwnoloc: replace ucdp_2yrs=1 if year>=ucdp_end_alt+2
			bys gwnoloc: replace ucdp_2yrs=0 if ucdp==1

	cap	gen ucdp_3yrs=0
			bys gwnoloc: replace ucdp_3yrs=. if ucdp_ever==0
			bys gwnoloc: replace ucdp_3yrs=1 if year>=ucdp_end_alt+3
			bys gwnoloc: replace ucdp_3yrs=0 if ucdp==1

		bys gwnoloc: egen ucdp_first_start_alt = min(ucdp_start_alt) if ucdp_start_alt!=.
		
		foreach n of numlist 1 2 3 {
			replace ucdp_`n'yrs = . if year<ucdp_first_start_alt
		}

		drop ucdp_first_start_alt
		
		la var ucdp_0yrs "In armed conflict (UCDP)"
		la var ucdp_1yrs "One year after end of armed conflict (UCDP)"
		la var ucdp_2yrs "Two years after end of armed conflict (UCDP)"
		la var ucdp_3yrs "Three years after end of armed conflict (UCDP)"
			
			
	
	* Generate interaction terms
	
		foreach x in itotal_compound_K iactual_civilian_total_K iany_demo_all_max_dum {
	cap		gen ipema_`x'=ipema_any_demo_assist_dum_2l * `x'_2l
		}
	
		foreach x in iactual_civilian_total_K iany_demo_all_max_dum {
	cap		gen itotal_`x'=itotal_compound_K_2l * `x'_2l
		}
	
	cap	gen iany_iactual_civilian_total_K = iany_demo_all_max_dum_2l * iactual_civilian_total_K_2l
	
		la var ipema_itotal_compound_K "Democracy mandate $\times$ \# of uniformed personnel"
		la var ipema_iactual_civilian_total_K "Democracy mandate $\times$ \# of civilian personnel"
		la var ipema_iany_demo_all_max_dum "Dmocracy mandate $\times$ any democracy activities"
		
		la var itotal_iactual_civilian_total_K "\# of uniformed personnel $\times$ \# of civilian personnel"
		la var itotal_iany_demo_all_max_dum "\# of uniformed personnel $\times$ any democracy activities"
		
		la var iany_iactual_civilian_total_K "Any democracy activities $\times$ \# of civilian personnel"
		
	
	
	
	* Generate version of uniformed personnel for country-years with data on civilian personnel
		
	cap	gen itotal_compound_K_2l_civ = itotal_compound_K_2l
			replace itotal_compound_K_2l_civ=. if iactual_civilian_total_K_2l==.
			la var itotal_compound_K_2l_civ "\# of uniformed personnel (restricted sample)"

	
	
	
	* Code changes in number of democracy mandate components over time
		
	cap	gen ipema_any_demo_more_2l = 0
			replace ipema_any_demo_more_2l = 1 if ipema_any_demo_agg_2l>ipema_any_demo_agg_3l & ipema_any_demo_agg_2l!=. & ipema_any_demo_agg_3l!=.

			la var ipema_any_demo_more_2l "More democracy mandate components than previous year (2 yr lag)"
			
	cap	gen ipema_any_demo_less_2l = 0
			replace ipema_any_demo_less_2l = 1 if ipema_any_demo_agg_2l<ipema_any_demo_agg_3l & ipema_any_demo_agg_2l!=. & ipema_any_demo_agg_3l!=.

			la var ipema_any_demo_less_2l "Fewer democracy mandate components than previous year (2 yr lag)"

	cap	gen ipema_any_demo_diff_2l = ipema_any_demo_agg_2l-ipema_any_demo_agg_3l
	cap		gen ipema_any_demo_diff=ipema_any_demo_diff_2l[_n+2]
			
			la var ipema_any_demo_diff "Change in \# of mandated democracy tasks from previous year"
			la var ipema_any_demo_diff_2l "Change in \# of mandated democracy tasks from previous year (2 yr lag)"
				

	
	
	* Relabel variables for parsimony in tables and figures
	
		la var v2x_polyarchy_2l "Electoral democracy (V-Dem)"
		la var v2x_polyarchy_3l "Electoral democracy (V-Dem, lagged)"
		la var v2x_polyarchy_4l "Electoral democracy (V-Dem, +2 lags)"
		
		la var iany_demo_all_max_dum_2l "Any democracy activities"
		la var iany_demo_all_max_dum_3l "Any democracy activities (+1 lag)"
		la var iany_demo_all_max_dum_4l "Any democracy activities (+2 lags)"
		
		la var iany_demo_rel_all_max_dum_2l "Any democracy-related activities"
		la var iall_demo_max_2l  "\# of distinct democracy activities"
		la var idemo_all_max_dum_2l "Any democratic institution activities"
		la var ielections_all_max_dum_2l "Any election activities"
		la var iparties_all_max_dum_2l "Any political party activities"
		la var ivoters_all_max_dum_2l "Any voter activities"
		
		la var iany_demo_engage_max_dum_2l "Any democracy engagement with host state"
		la var iany_demo_bypass_max_dum_2l "Any democracy bypassing of host state"
		
		la var iall_demo_engage_max_2l "# of distinct democracy activities engaging host state"
		la var iall_demo_bypass_max_2l "# of distinct democracy activities bypassing host state"
				
		la var ipema_any_demo_assist_dum_2l "Democracy mandate"
		la var ipema_any_demo_assist_dum_3l "Democracy mandate (+1 lag)"
		la var ipema_any_demo_assist_dum_4l "Democracy mandate (+2 lags)"
		
		la var ipema_any_demo_rel_assist_dum_2l "Democracy-related mandate"
		
		la var ipema_any_demo_assdiv_2l "\% democracy mandates in other missions" 
				
		la var ipema_any_demo_diff_2l "Change in \# of mandated democracy tasks from previous year"
		la var ipema_any_demo_agg_2l "\# of distinct democracy tasks in mandate"
		
		la var iactual_civilian_total_K_2l "\# of civilian personnel"
		la var iactual_civilian_total_K_3l "\# of civilian personnel (+1 lag)"
		la var iactual_civilian_total_K_4l "\# of civilian personnel (+2 lags)"
				
		la var itotal_compound_K_2l "\# of uniformed personnel"
		la var itotal_compound_K_3l "\# of uniformed personnel (+1 lag)"
		la var itotal_compound_K_4l "\# of uniformed personnel (+2 lags)"
		
		la var itotal_compound_K_2l_civ "\# of uniformed personnel (restricted sample)"
				
		la var iwdi_pop_3l "Population"
		la var iwdi_oda_3l "Foreign aid"
		la var iwdi_gdppc_3l "GDP per capita"
		la var iunhcr_ref_idp_3l "Refugees"
		la var iwdi_literacy_3l "Literacy"
		la var iwdi_fuel_3l "Fuel exports"
		
		la var iwdi_pop_4l "Population (+1 lag)"
		la var iwdi_oda_4l "Foreign aid (+1 lag)"
		la var iwdi_gdppc_4l "GDP per capita (+1 lag)"
		la var iunhcr_ref_idp_4l "Refugees (+1 lag)"
		la var iwdi_literacy_4l "Literacy (+1 lag)"
		la var iwdi_fuel_4l "Fuel exports (+1 lag)"

		la var iwdi_pop_5l "Population (+2 lags)"
		la var iwdi_oda_5l "Foreign aid (+2 lags)"
		la var iwdi_gdppc_5l "GDP per capita (+2 lags)"
		la var iunhcr_ref_idp_5l "Refugees (+2 lags)"
		la var iwdi_literacy_5l "Literacy (+2 lags)"
		la var iwdi_fuel_5l "Fuel exports (+2 lags)"

	
	
	
	* Drop observations before 1991 (accounting for lags)
	
		drop if year<1991
	
	
	
	
	* Set globals
			
		gl ictrls_2l_varying iwdi_pop_2l iwdi_oda_2l iwdi_gdppc_2l iunhcr_ref_idp_2l iwdi_literacy_2l iwdi_fuel_2l 

		gl ictrls_3l_varying iwdi_pop_3l iwdi_oda_3l iwdi_gdppc_3l iunhcr_ref_idp_3l iwdi_literacy_3l iwdi_fuel_3l

		gl ictrls_4l_varying iwdi_pop_4l iwdi_oda_4l iwdi_gdppc_4l iunhcr_ref_idp_4l iwdi_literacy_4l iwdi_fuel_4l

		gl ictrls_5l_varying iwdi_pop_5l iwdi_oda_5l iwdi_gdppc_5l iunhcr_ref_idp_5l iwdi_literacy_5l iwdi_fuel_5l
